//
//  PushAnimation.swift
//  funcode
//
//  Created by Wan Jiading on 2016/11/23.
//  Copyright © 2016年 WanJiading. All rights reserved.
//

import UIKit

class PushAnimation: NSObject, UIViewControllerAnimatedTransitioning {
	
	
	func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
		return 0.25
	}
	
	func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
		DebugLog("animateTransition")
		
		let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
		let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
		
		// 转场动画是两个控制器视图的动画，需要一个containerView作为“舞台”
		let contrainerView = transitionContext.containerView
		contrainerView.insertSubview(toVC.view, belowSubview: fromVC.view)
		
		let duration = self.transitionDuration(using: transitionContext)
		
		// 执行动画，让fromVC的view移动到屏幕最右侧
		UIView.animate(withDuration: duration, animations: {
//			fromVC.view.transform = CGAffineTransform.init(translationX: UIScreen.main.bounds.width, y: 0)
			
			toVC.view.transform = CGAffineTransform.init(translationX: 0, y: 0)
			
		}, completion: { (finished) in
			// 当动画执行完时，这个方法必须要调用，否则系统会认为你的其余操作都在动画执行过程中
			transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
		})
	}
	
}
